1 using System;
2 using
UnityEngine;
3 using
Object = UnityEngine.Object;
4
5 // same
as Triangles but creates quads instead which generally
6 // saves fillrate at the expense
for more triangles to issue
7 namespace
UnityStandardAssets.ImageEffects
8 {
9     
class Quads
10     {
11         
static Mesh[] meshes;
12         
static int currentQuads = 0;
13
14         
static bool HasMeshes ()
15         {
16             
if (meshes == null)
17                 
return false;
18             
foreach (Mesh m in meshes)
19                 
if (null == m)
20                     
return false;
21             
return true;
22         }
23
24
25         
public static void Cleanup ()
26         {
27             
if (meshes == null)
28                 
return;
29
30             
for (int i = 0; i < meshes.Length; i++)
31             {
32                 
if (null != meshes[i])
33                 {
34                     Object.DestroyImmediate (meshes[i]);
35                     meshes[i] =
null;
36                 }
37             }
38             meshes =
null;
39         }
40
41
42         
public static Mesh[] GetMeshes ( int totalWidth, int totalHeight)
43         {
44             
if (HasMeshes () && (currentQuads == (totalWidth * totalHeight))) {
45                 
return meshes;
46             }
47
48             
int maxQuads = 65000 / 6;
49             
int totalQuads = totalWidth * totalHeight;
50             currentQuads = totalQuads;
51
52             
int meshCount = Mathf.CeilToInt ((1.0f * totalQuads) / (1.0f * maxQuads));
53
54             meshes =
new Mesh [meshCount];
55
56             
int i = 0;
57             
int index = 0;
58             
for (i = 0; i < totalQuads; i += maxQuads)
59             {
60                 
int quads = Mathf.FloorToInt (Mathf.Clamp ((totalQuads-i), 0, maxQuads));
61
62                 meshes[index] = GetMesh (quads, i, totalWidth, totalHeight);
63                 index++;
64             }
65
66             
return meshes;
67         }
68
69         
static Mesh GetMesh (int triCount, int triOffset, int totalWidth, int totalHeight)
70         {
71             
var mesh = new Mesh ();
72             mesh.hideFlags = HideFlags.DontSave;
73
74             
var verts = new Vector3[triCount * 4];
75             
var uvs = new Vector2[triCount * 4];
76             
var uvs2 = new Vector2[triCount * 4];
77             
var tris = new int[triCount * 6];
78
79             
for (int i = 0; i < triCount; i++)
80             {
81                 
int i4 = i * 4;
82                 
int i6 = i * 6;
83
84                 
int vertexWithOffset = triOffset + i;
85
86                 
float x = Mathf.Floor (vertexWithOffset % totalWidth) / totalWidth;
87                 
float y = Mathf.Floor (vertexWithOffset / totalWidth) / totalHeight;
88
89                 Vector3 position =
new Vector3 (x * 2 - 1, y * 2 - 1, 1.0f);
90
91                 verts[i4 +
0] = position;
92                 verts[i4 +
1] = position;
93                 verts[i4 +
2] = position;
94                 verts[i4 +
3] = position;
95
96                 uvs[i4 +
0] = new Vector2 (0.0f, 0.0f);
97                 uvs[i4 +
1] = new Vector2 (1.0f, 0.0f);
98                 uvs[i4 +
2] = new Vector2 (0.0f, 1.0f);
99                 uvs[i4 +
3] = new Vector2 (1.0f, 1.0f);
100
101                 uvs2[i4 +
0] = new Vector2 (x, y);
102                 uvs2[i4 +
1] = new Vector2 (x, y);
103                 uvs2[i4 +
2] = new Vector2 (x, y);
104                 uvs2[i4 +
3] = new Vector2 (x, y);
105
106                 tris[i6 +
0] = i4 + 0;
107                 tris[i6 +
1] = i4 + 1;
108                 tris[i6 +
2] = i4 + 2;
109
110                 tris[i6 +
3] = i4 + 1;
111                 tris[i6 +
4] = i4 + 2;
112                 tris[i6 +
5] = i4 + 3;
113
114             }
115
116             mesh.vertices = verts;
117             mesh.triangles = tris;
118             mesh.uv = uvs;
119             mesh.uv2 = uvs2;
120
121             
return mesh;
122         }
123
124     }
125 }


Gõ tìm kiếm nhanh...